package com.webchat.pay.service;

import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.domain.vo.request.ValidateAccountPasswordReqVO;
import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
import com.webchat.pay.config.exception.PaymentAccessAuthException;
import com.webchat.rmi.user.UserServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class AccountService {

    @Autowired
    private UserServiceClient userServiceClient;

    public UserBaseResponseInfoVO accountInfo(String account) {
        APIResponseBean<UserBaseResponseInfoVO> responseBean = userServiceClient.userInfo(account);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        /**
         * 降级策略 TODO
         */
        return null;
    }

    public Map<String, UserBaseResponseInfoVO> batchGet(Set<String> userIds) {
        APIResponseBean<Map<String, UserBaseResponseInfoVO>> responseBean = userServiceClient.batchGet(userIds);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        // TODO
        log.error("UserServiceClient.batchGet error: {}", responseBean);
        return Collections.emptyMap();
    }


    /**
     * 校验用户密码是否正确
     *
     * @param account
     * @param password
     * @return
     */
    public boolean validateAccountPassword(String account, String password) {

        ValidateAccountPasswordReqVO validateAccountPasswordReqVO = new ValidateAccountPasswordReqVO(account, password);
        APIResponseBean<Boolean> responseBean = userServiceClient.validateAccountPassword(validateAccountPasswordReqVO);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new PaymentAccessAuthException(responseBean.getMsg());
    }

}
